************************************************************************;
*This do file calculate the moments used for the estimation of the model 
************************************************************************;

clear all
set obs 1
forvalues j = 1(1)36{
gen mom`j' = .
}

save collect_moments.dta, replace

use $scratch/final_data_for_model.dta, clear

recode H1WP2 (0=1) (1=0) , gen(authoritarian_parents)

factor cogn1_tilde cogn2_tilde cogn3_tilde cogn4_tilde cogn5_tilde, factors(1)
predict skills_hat if e(sample), bartlett
qui sum skills_hat if class==9
replace skills_hat = (skills-r(mean))/r(sd)

factor    inv4 inv6   inv9  , factors(1)
predict inv if e(sample) , bartlett

factor    inv4_t2 inv6_t2   inv9_t2  , factors(1)
predict inv_t2 if e(sample) , bartlett

local ind = 1
foreach x in H1WP1 H1WP2 H1WP3  H1WP6  {
recode `x' (0=1) (1=0), gen(author_proxy`ind')
local ++ind
}

local ind = 1
foreach x in  H2WP1 H2WP2 H2WP3  H2WP6  {
recode `x' (0=1) (1=0), gen(author_proxy`ind'_t2)
local ++ind
}


factor author_proxy1 author_proxy2 author_proxy3 author_proxy4  , factors(1) ml
predict author_factor if e(sample), bartlett
qui sum author_factor, de
gen author_factor_sd = (author_factor-r(mean))/r(sd)

factor author_proxy1_t2 author_proxy2_t2 author_proxy3_t2 author_proxy4_t2  , factors(1) ml
predict author_factor_t2 if e(sample), bartlett
qui sum author_factor_t2, de
gen author_factor_t2_sd = (author_factor_t2-r(mean))/r(sd)


forvalues j = 1(1)5{
gen mean_peer_cogn`j'_tilde = (mean_peer_cogn`j'-mu_cogn`j')/lambda_cogn`j'
}

forvalues j = 2(1)5{
gen mean_peer_cogn`j'_tilde_t2 = (mean_peer_cogn`j'_t2 - mu_cogn`j')/lambda_cogn`j'
}


factor mean_peer_cogn2_tilde mean_peer_cogn1_tilde   mean_peer_cogn3_tilde mean_peer_cogn4_tilde mean_peer_cogn5_tilde, factors(1)
predict peers_skills_hat if e(sample), bartlett
qui sum peers_skills_hat
replace peers_skills_hat = (peers_skills_hat-r(mean))/r(sd)

factor mean_peer_cogn2_tilde_t2  mean_peer_cogn3_tilde_t2 mean_peer_cogn4_tilde_t2 mean_peer_cogn5_tilde_t2, factors(1)
predict peers_skills_t2_hat if e(sample), bartlett
qui sum peers_skills_t2_hat
replace peers_skills_t2_hat = (peers_skills_t2_hat-r(mean))/r(sd)


forvalues j = 2(1)5{
gen cogn`j'_tilde_t2 = (cogn`j'_t2 - mu_cogn`j')/lambda_cogn`j'
}

factor cogn2_tilde_t2  cogn3_tilde_t2 cogn4_tilde_t2 cogn5_tilde_t2, factors(1)
predict skills_t2_hat if e(sample), bartlett


qui sum author_factor_sd, de 
gen d_author_factor_median = ( author_factor_sd > r(p50)) if author_factor_sd!=.
qui sum author_factor_t2_sd, de 
gen d_author_factor_median_t2 = ( author_factor_t2_sd > r(p50)) if author_factor_t2_sd!=.

gen delta_author_factor = author_factor_t2_sd - author_factor_sd

gen delta_author = d_author_factor_median_t2 - d_author_factor_median
gen delta_inv = inv_t2 - inv
gen delta_skills = skills_t2_hat - skills_hat
gen delta_peer_skills = peers_skills_t2_hat - peers_skills_hat


preserve

keep if school_size>=200
keep if class >=9 & class<=12
*Keep main High-Schools in the sample;
keep if  idschool<100 | (idschool>=200 & idschool<300) 



********************************;
*1) Mean of PS in the sample;
********************************;

sum authoritarian_parents if class<=11
gen mom1  = r(mean)

********************************;
*2) Regression of PS on state variables;
********************************;

xtset idgrade
xtreg d_author_factor_median skills_hat peers_skills_hat , fe 

gen mom2  = _b[skills_hat]
gen mom3  = _b[peers_skills_hat]

************************************************;
* Dynamic Effects on Peers;
************************************************;

xtreg skills_t2_hat skills_hat peers_skills_hat d_author_factor_median  if class<=11 , fe vce(cluster idschool)

gen mom4  = _b[skills_hat]
gen mom5  = _b[peers_skills_hat]
gen mom6  = _b[d_author_factor_median]

xtreg skills_t2_hat skills_hat peers_skills_hat  if d_author_factor_median==0   & class<=11 , fe vce(cluster idschool)

gen mom7  = _b[skills_hat]
gen mom8  = _b[peers_skills_hat]

xtreg skills_t2_hat skills_hat peers_skills_hat  if d_author_factor_median==1   & class<=11 , fe vce(cluster idschool)

gen mom9  = _b[skills_hat]
gen mom10  = _b[peers_skills_hat]

keep mom*
keep in 1

append using collect_moments.dta

save collect_moments.dta, replace

restore


preserve

keep if class >=9 & class<=11
*Keep only saturated High-Schools in the sample;
keep if saturated==1

xtset idgrade

foreach x in skills_hat peers_skills_hat{
gen `x'_miss = `x' 
qui sum `x'
replace `x'_miss = r(mean) if `x'==.
gen d_`x'_miss = (`x'==.)
}

xtreg peers_skills_t2_hat  skills_hat_miss peers_skills_hat_miss d_skills_hat_miss d_peers_skills_hat_miss d_author_factor_median , fe vce(cluster idschool)
gen mom11  = _b[skills_hat]
gen mom12  = _b[peers_skills_hat]
gen mom13  = _b[d_author_factor_median]

xtreg peers_skills_t2_hat skills_hat_miss peers_skills_hat_miss d_skills_hat_miss d_peers_skills_hat_miss  if d_author_factor_median==0 , fe vce(cluster idschool)
gen mom14  = _b[skills_hat]
gen mom15  = _b[peers_skills_hat]

xtreg peers_skills_t2_hat skills_hat_miss peers_skills_hat_miss d_skills_hat_miss d_peers_skills_hat_miss  if d_author_factor_median==1 , fe vce(cluster idschool)
gen mom16  = _b[skills_hat]
gen mom17  = _b[peers_skills_hat]

keep mom*
keep in 1

append using collect_moments.dta

save collect_moments.dta, replace

restore

preserve
keep if school_size>=200
keep if class >=9 & class<=12
*Keep main High-Schools in the sample;
keep if  idschool<100 | (idschool>=200 & idschool<300) 

xtset idgrade

*5) Regression of Investments on state variables conditioning on Parenting Style=0;
xtreg inv skills_hat peers_skills_hat if d_author_factor_median==0 & class<=11, fe vce(cluster idschool)
gen mom18  = _b[skills_hat]
gen mom19  = _b[peers_skills_hat]

*6) Regression of Investments on state variables conditioning on Parenting Style=1;
xtreg inv skills_hat peers_skills_hat if d_author_factor_median==1 & class<=11 , fe vce(cluster idschool)
gen mom20  = _b[skills]
gen mom21  = _b[peers_skills]



*7) Dynamics of Skills;
egen skills_mean = rowmean(cogn1_tilde cogn2_tilde cogn4_tilde cogn5_tilde)

local ind_mom = 22
forvalues j = 9(1)12{
qui sum skills_mean if class==`j'
gen mom`ind_mom'  = r(mean)
local ind_mom = `ind_mom ' + 1  
}

qui sum inv if class<=11
gen inv_mean0 = inv - r(mean) if class<=11

*8) Mean Parental Investments conditioning on Parenting Style=0;
sum inv_mean0 if authoritarian_parents==0
gen mom`ind_mom'  = r(mean)
local ind_mom = `ind_mom ' + 1 

*9) Mean Parental Investments conditioning on Parenting Style=1;
sum inv_mean0 if authoritarian_parents==1
gen mom`ind_mom'  = r(mean)
local ind_mom = `ind_mom ' + 1

*10) Mean Number of Friends;
egen n_friends = rowmax( n_peers_cogn1 n_peers_cogn2 n_peers_cogn3 n_peers_cogn4 n_peers_cogn5)
sum n_friends if n_friends>0 & class<=11 & inv!=. & peers_skills_hat!=.
gen mom`ind_mom'  = r(mean)
local ind_mom = `ind_mom ' + 1

keep mom*
keep in 1

append using collect_moments.dta

save collect_moments.dta, replace

restore

preserve

keep if class >=9 & class<=11
keep if saturated==1

reg delta_author delta_skills  delta_peer_skills ,  vce(cluster idschool) 
gen mom`ind_mom'  = _b[delta_skills]
local ind_mom = `ind_mom ' + 1
gen mom`ind_mom'  = _b[delta_peer_skills]
local ind_mom = `ind_mom ' + 1


reg delta_inv  authoritarian_parents  ,  vce(cluster idschool)
gen mom`ind_mom'  = _b[_cons]
local ind_mom = `ind_mom ' + 1
gen mom`ind_mom'  = _b[authoritarian_parents]
local ind_mom = `ind_mom ' + 1

append using collect_moments.dta

save collect_moments.dta, replace

restore



preserve

keep if school_size>=200
keep if class >=9 & class<=12
*Keep main High-Schools in the sample;
keep if  idschool<100 | (idschool>=200 & idschool<300) 
drop _merge
merge m:1 idschool using $scratch\schools_neighborhoods_id.dta
drop if _merge==2
drop if quartiles_schools==.

collapse authoritarian_parents if class<=11, by(quartiles_schools)
sort quartiles_schools 
keep authoritarian_parents
xpose, clear

local ind = 33
forvalues j = 1(1)4{

rename v`j' mom`ind'
local ++ind
}

append using collect_moments.dta

save collect_moments.dta, replace

restore




preserve

keep if school_size>=200
keep if class >=9 & class<=11
*Keep main High-Schools in the sample;
keep if  idschool<100 | (idschool>=200 & idschool<300) 
drop _merge
merge m:1 idschool using $scratch\schools_neighborhoods_id.dta
drop if _merge==2
drop if quartiles_schools==.

collapse authoritarian_parents , by(quartiles_schools class)
order quartiles_schools  class
sort quartiles_schools  class
reshape wide author , i(quartiles) j(class)
keep authoritarian_parents* 
outsheet using "$matlab_dir\mean_PS_between_class.txt", nonames replace

restore




*******************************************;
* Average Family Income in Neighborhoods;
*******************************************;


preserve

keep if school_size>=200
keep if class >=9 & class<=12
*Keep main High-Schools in the sample;
keep if  idschool<100 | (idschool>=200 & idschool<300) 
drop _merge
merge m:1 idschool using $scratch\schools_neighborhoods_id.dta
drop if _merge==2
drop if quartiles_schools==.


collapse real_family_income , by(quartiles_schools)

keep real_family_income

xpose, clear

outsheet using "$matlab_dir\family_income_neighborhoods.txt", nonames replace

restore


*******************************************;
* Median Family Income in Neighborhoods;
*******************************************;

preserve

keep if school_size>=200
keep if class >=9 & class<=12
*Keep main High-Schools in the sample;
keep if  idschool<100 | (idschool>=200 & idschool<300) 
drop _merge
merge m:1 idschool using $scratch\schools_neighborhoods_id.dta
drop if _merge==2
drop if quartiles_schools==.


collapse (median) real_family_income , by(quartiles_schools)


restore




*******************************************;
*Tabulate Moments for Estimation;
*******************************************;

use collect_moments.dta, clear

collapse mom*

gen i = _n

reshape long mom@ , i(i) j(order_mom)

drop i
sort order_mom
drop order_mom

outsheet using "$matlab_dir\moments.txt", nonames replace
 
